﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeDynamiksEngine;
using MESCodeFile;
using LiveNotificationSystem;

namespace mes_tublay
{
    public partial class frmeBPLSsearch : Form
    {
        DataCache result1, entryid;
        int eid;
        string[] n = new string[0];
        public frmeBPLSsearch()
        {
            InitializeComponent();
            InitializeFrameSliderForm();
        }
        private void InitializeForm()
        {
            btnSearch.Click += new EventHandler(btnSearch_Click);
            gridresult.DoubleClick += new EventHandler(gridresult_DoubleClick);
        }

        void gridresult_DoubleClick(object sender, EventArgs e)
        {
            int sel = gridresult.CurrentRow.Index;
            int aydi = int.Parse(gridresult.Rows[sel].Cells[0].Value.ToString());
            frmTreasuryeBPLS frmTeBPLS = new frmTreasuryeBPLS();
            //GET META DATA TO FORWARD TO eBPLS form
            CodeScript s = new CodeScript("GETMETADATA");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                        Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            string searchstring = txtOwnerName.Text;
            s.Add(CodeIns.DBU_QUERY, "Select fMeta from tblnotificationservice where fID=" + aydi);
            //REMOVE ENTRY IN NOTIFICATION
            s.Add(CodeIns.DBU_UPDATE, "tblnotificationservice", new string[] { "fStatus" }, new object[] { "'PULLED'" }, "fID='"+eid+"'");
            //END
            s.Add(CodeIns.DBU_END);

            CodeProcessor cp = new CodeProcessor(ThreadingMode.Exclusive);
            cp.ProcessCompleted += new EventHandler(delegate
            {
                cp.SafeCall(this, delegate
                {
                    if (cp.State == ProcessorFlag.DataRdyErr ||
                        cp.State == ProcessorFlag.Error ||
                        cp.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Danger", cp.GetProcessorMessage);
                        return;
                    }
                    cp.DataFetch(out entryid);
                    entryid.SetActivePage(0);
                    string md = entryid.GetValue(0, "fMeta").ToString();
                    n = md.Split('|');
                    frmTeBPLS.SetAppNumber( n[0].ToString(), n[1].ToString(), n[2].ToString(), n[3].ToString());
                    frmTeBPLS.Initialize("Treasury eBPLS");
                    Common.PARENT.AddForm(frmTeBPLS.PanelForm, "Treasury eBPLS");
                });
            });
            cp.Run(s);
        }

        void btnSearch_Click(object sender, EventArgs e)
        {
            CodeScript s = new CodeScript("INITIALCODE");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                        Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            string searchstring = txtOwnerName.Text;
            s.Add(CodeIns.DBU_QUERY, "Select fID as ENTRY_NO, fMsg as MESSAGE from tblnotificationservice where fMsgType='TREASURY_DEPT_ASSESSMENT' and fStatus='PUSHED' and fMeta like '" + searchstring + "%'");
            s.Add(CodeIns.DBU_END);

            CodeProcessor cp = new CodeProcessor(ThreadingMode.Exclusive);
            cp.ProcessCompleted += new EventHandler(delegate
            {
                cp.SafeCall(this, delegate
                {
                    if (cp.State == ProcessorFlag.DataRdyErr ||
                        cp.State == ProcessorFlag.Error ||
                        cp.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Danger", cp.GetProcessorMessage);
                        return;
                    }
                    cp.DataFetch(out result1);
                    result1.SetActivePage(0);
                    gridresult.DataSource = result1.ExportPage();
                    eid = int.Parse(result1.GetValue(0, "ENTRY_NO").ToString());
                    gridresult.Columns[0].Width = (int)((float)gridresult.Width * 0.3f);
                });
            });
            cp.Run(s);
        }
    }
}
